import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import PublicityPage from "./management-system/components/pub/PublicityPage.vue";
import LoginPage from "./management-system/components/login/LoginPage.vue";
import NotFound from "./management-system/components/NotFound.vue";
import OrganizationManagement from "./tool/menu/setting/organization/OrganizationManagement.vue";
import AccountManagerPage from "./tool/menu/setting/account/AccountManagerPage.vue";
import UserPage from "./tool/menu/user/UserPage.vue";
import HomePage from "./tool/menu/home/HomePage.vue";
import Index from "./management-system/components/Index.vue";
import {ref} from "vue";
import {userStore} from "./store/user";
import pina from "./pinia";
import MenuConfiguration from "./tool/menu/configuration/MenuConfiguration.vue";
import RoleManagement from "./tool/menu/setting/role/RoleManagement.vue";
import OrgManagement from "./tool/menu/setting/org/OrgManagement.vue";
import {useRouterStore} from "./store/router";


const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'publicity',
        component: PublicityPage
    },
    {
        path: '/login',
        name: 'login',
        component: LoginPage
    },
    {
        path: '/index',
        name: 'index',
        component: Index,
        children: [
            {
                path: 'home',
                name: 'home',
                component: HomePage,
            },
           /* {
                path: 'menu/userPage',
                name: 'userPage',
                component: UserPage,
            },
            {
                path: 'menu/account',
                name: 'account',
                component: AccountManagerPage,
            },
            {
                path: 'menu/organization',
                name: 'organization',
                component: OrganizationManagement,
            },
            {
                path: 'menu/orgManage',
                name: 'orgManage',
                component: OrgManagement,
            },
            {
                path: 'menu/roleManagement',
                name: 'roleManagement',
                component: RoleManagement,
            },
            {
                path: 'menu/menuConfiguration',
                name: 'menuConfiguration',
                component: MenuConfiguration,
            },*/
            {
                path: '/index:afterUser(.*)',
                component: NotFound
            },
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach((to, from) => {
    let user = userStore(pina);
    let path = to.path
    if (path.startsWith('/index') && user.userInfo.token === '') {
        return {name: 'login'}
    }
    if (path === '/login' || path === '/') {
        if (user.userInfo.token !== '') {
            return false
        }
    }
})

const registerRouteFresh = ref(true)
router.beforeEach(async (to, from, next) => {
    let datas = JSON.parse(sessionStorage.getItem('route_cache') as string)
    if (registerRouteFresh.value && datas != null) {
        await datas.forEach(element => {
            router.addRoute(
                element.root,
                {
                    path: element.obj.path,
                    component: ()=>import(`./management-system/components/window/editor/default/${element.obj.component.__name}.vue`)
                    // component: element.obj.component.component
                }
            )
        })
        next({path: to.path})
        registerRouteFresh.value = false
    } else {
        next()
    }
})


// 初始化路由
const registerMenuRoute = ref(true)
router.beforeEach(async (to, from, next) => {
    let menus = useRouterStore(pina).menu_route
    if (registerMenuRoute.value && menus != null) {
        await menus.then(data=>{
            data.forEach(element => {
                router.addRoute(
                    'index',
                    {
                        path: element.path,
                        name:element.name,
                        component: ()=>import(`./${element.component}`)
                    }
                )
            })
        })
        next({path: to.path})
        registerMenuRoute.value = false
    } else {
        next()
    }
    // next()
})


export default router